<?php
namespace  app\behaviors;

use yii\base\Behavior;
use yii\db\BaseActiveRecord;

class Behaviors extends Behavior {
    public function events() {
        return array_merge(
            parent::events(), [
                BaseActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
                BaseActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
                BaseActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',
                BaseActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave',
            ]
        );
    }

    public function beforeSave($event) {
        $Sender = $event->sender;
        if (!$Sender->hasAttribute('ctime')) {
            return $event->isValid;
        }

        if ($Sender->isNewRecord) {
            $Sender->ctime = date('Y-m-d H:i:s');
        }
        $Sender->utime = date('Y-m-d H:i:s');
    }

    public function afterSave($event) {

    }
}